package com.wu.llpsw.item.wushu;

import com.wu.llpsw.data.playerData;
import com.wu.llpsw.networking.ModMessage;
import com.wu.llpsw.networking.packet.backYuanLiMaxPacket;
import com.wu.llpsw.networking.packet.backYuanLiNowPacket;
import com.wu.llpsw.yuanli.energy.yuanliManager;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

//烈火掌
public class lie_huo_zhang extends Item {
    public int setup=10;//启动技能需要的最少元力
    public float attackValue=2.0f;//基础攻击力
    public lie_huo_zhang(Properties p_41383_) {
        super(p_41383_);
    }

    //获取防火效果
    @NotNull
    @Override
    public InteractionResultHolder<ItemStack> use(@NotNull Level world, @NotNull Player player, @NotNull InteractionHand hand) {
        if (!world.isClientSide) {

            if(yuanliManager.getNowYuanLi(player)>=setup){
                yuanliManager.use(player,-setup);

                player.addEffect(new MobEffectInstance(MobEffects.GLOWING, 20 * 5, 1));
                player.addEffect(new MobEffectInstance(MobEffects.FIRE_RESISTANCE, 20 * 60, 1));
                player.getCooldowns().addCooldown(this,20*100);
            }
        }
        return InteractionResultHolder.success(player.getItemInHand(hand));
    }

    //攻击生物
    @Override
    public boolean hurtEnemy(ItemStack itemStack, LivingEntity attacked, LivingEntity attack) {
        Player player=(Player) attack;

        if (yuanliManager.getNowYuanLi(player)>=setup){
            yuanliManager.use(player,-setup);
            ModMessage.sendToPlayer(new backYuanLiMaxPacket(yuanliManager.getMaxYuanLi(player)),(ServerPlayer) player);
            ModMessage.sendToPlayer(new backYuanLiNowPacket(yuanliManager.getNowYuanLi(player)),(ServerPlayer)player);
            player.addEffect(new MobEffectInstance(MobEffects.GLOWING, 20 * 5, 1));
            //生成粒子效果
            ServerLevel serverWorld = (ServerLevel) attacked.getCommandSenderWorld();
            double x1 = player.getX();
            double y1 = player.getY()+1.0f;
            double z1 = player.getZ();
            double xa= player.getLookAngle().x;
            double ya= player.getLookAngle().y;
            double za= player.getLookAngle().z;
            for (float i=0.0f;i<10.0f;i+=1.0f){
                serverWorld.sendParticles(ParticleTypes.FLAME, x1+xa*i, y1+ya*i, z1+za*i, 7, 0.0f, 0, 0, 0);
            }


            //着火
            attacked.setSecondsOnFire(10);
            //造成伤害
            attacked.hurt(DamageSource.GENERIC, (float) (playerData.getAttack(player)+attackValue));
        }
        return true;
    }


    //添加描述
    @Override
    public void appendHoverText(ItemStack p_41421_, @Nullable Level p_41422_, List<Component> p_41423_, TooltipFlag p_41424_) {
        p_41423_.add(new TranslatableComponent("tooltip.wu.lie_huo_zhang.tooltip"));
    }
}
